package com.ebeitech.auction.view.auction;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ebeitech.auction.R;
import com.ebeitech.auction.bean.Auction;
import com.ebeitech.auction.bean.AuctionBidder;
import com.ebeitech.auction.bean.AuctionPhase;
import com.ebeitech.auction.bean.LandInfo;
import com.ebeitech.auction.constants.CommonConstants;
import com.ebeitech.auction.domain.ChatMessage;
import com.ebeitech.auction.http.EbeiErrorCode;
import com.ebeitech.auction.http.HttpService;
import com.ebeitech.auction.http.service.HttpAuctionServiceRx;
import com.ebeitech.auction.view.homepage.BaseActivity;
import com.ebeitech.library.constant.MessageConstants;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.ui.CommonItemView;
import com.ebeitech.library.ui.NumberVariateView;
import com.ebeitech.library.ui.dialog.CommonAlertDialogFragment;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.ViewUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by alice on 2017/10/30.
 */
public class AuctionBidActivity extends BaseActivity {

	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.view_bidder)
	CommonItemView viewBidder;
	@BindView(R.id.view_new_quote)
	CommonItemView viewNewQuote;
	@BindView(R.id.view_step)
	CommonItemView viewStep;
	@BindView(R.id.view_multiple)
	CommonItemView viewMultiple;
	@BindView(R.id.view_current_bid)
	CommonItemView viewCurrentBid;
	@BindView(R.id.view_ratio)
	CommonItemView viewRatio;
	@BindView(R.id.btn_submit)
	Button btnSubmit;
	EditText etMultiple;

	private Context mContext;
	private String mTitleName;

	private Auction mAuction;
	private String mUnit;

	private AuctionPhase mAuctionPhase;
	private AuctionBidder mAuctionBidder;

	private View mLastModifyView;
	private boolean mIsNeedChange;
	private String rateDemo;

	private List<AuctionBidder> mAuctionBidders;

	private List<String> auctionBidderNames;

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent != null) {
				String action = intent.getAction();
				if (MessageConstants.ACTION_MESSAGE_RECEIVE.equals(action)) {
					ChatMessage chatMessage = (ChatMessage) intent.getSerializableExtra(MessageConstants.MESSAGE);
					if (chatMessage != null) {
						// 先看是否是本房间的信息
						if (!StringUtil.getDefaultString(mAuction.getAuctionId()).equals(chatMessage.getAuctionId())) {
							return;
						}

						if (ChatMessage.MESSAGE_TYPE_OUR_PRICE_RECALL.equals(chatMessage.getMessageType())) {
							// 如果消息是撤回,就不管最高价,直接用撤回消息里返回的价格
							mAuctionPhase.setPhaseResult(chatMessage.getPrice());
							mAuctionPhase.setPhaseResultRate(chatMessage.getPhaseResultRate());
							mAuctionPhase.setPhaseOurResult(chatMessage.getPhaseOurResult());
							mAuctionPhase.setPhaseOurResultRate(chatMessage.getPhaseOurResultRate());
							AuctionBidder resultBidder = new AuctionBidder();
							resultBidder.setBidderAuctionCode(chatMessage.getAuctionBidderCode());
							resultBidder.setBidderName(chatMessage.getBidderName());
							mAuctionPhase.setResultBidder(resultBidder);
							updateView();
						} else if (ChatMessage.MESSAGE_TYPE_OUR_PRICE.equals(chatMessage.getMessageType())
								|| ChatMessage.MESSAGE_TYPE_THEIR_PRICE.equals(chatMessage.getMessageType())) {
							// 如果是递增,新的报价必须大于当前报价;递减的话必须小于
							boolean isValid = false;
							float oldResult = 0, newResult = 0;
							try {
								oldResult = Float.valueOf(mAuctionPhase.getPhaseResult());
								newResult = Float.valueOf(chatMessage.getPrice());
							} catch (Exception e) {
							}
							if (AuctionPhase.QUOTES_TYPE_ADD.equals(mAuctionPhase.getQuotesTypeCode()) && newResult > oldResult) {
								isValid = true;
							} else if (AuctionPhase.QUOTES_TYPE_MINUS.equals(mAuctionPhase.getQuotesTypeCode()) && newResult < oldResult) {
								isValid = true;
							}
							if (!isValid) {
								return;
							}

							// 如果消息是出价的消息,要更新现有的报价
							mAuctionPhase.setPhaseResult(chatMessage.getPrice());
							mAuctionPhase.setPhaseResultRate(chatMessage.getPhaseResultRate());
							AuctionBidder resultBidder = new AuctionBidder();
							resultBidder.setBidderAuctionCode(chatMessage.getAuctionBidderCode());
							resultBidder.setBidderName(chatMessage.getBidderName());
							mAuctionPhase.setResultBidder(resultBidder);
							if (ChatMessage.MESSAGE_TYPE_OUR_PRICE.equals(chatMessage.getMessageType())) {
								mAuctionPhase.setPhaseOurResult(chatMessage.getPrice());
								mAuctionPhase.setPhaseOurResultRate(chatMessage.getPhaseResultRate());
							}
							updateView();
						} else if (ChatMessage.MESSAGE_TYPE_PHASE_SWITCH.equals(chatMessage.getMessageType())) {
							// 如果消息是阶段切换的消息,要更新阶段
							mAuctionPhase.setPhaseState("0");
							List<AuctionPhase> auctionPhases = mAuction.getAuctionPhases();
							boolean isFirstBegin = false, isEnd = false;
							for (int i = 0; i < auctionPhases.size(); i++) {
								AuctionPhase auctionPhase = auctionPhases.get(i);
								if (StringUtil.getDefaultString(chatMessage.getNextPhaseId()).equals(auctionPhase.getPhaseId())) {
									// 看看是不是大于当前阶段
									if (i > 0 && !(mAuctionPhase.getPhaseSort() < auctionPhase.getPhaseSort())) {
										mAuctionPhase.setPhaseState("1");
										return;
									}

									auctionPhase.setPhaseState("1");
									auctionPhase.setPhaseResult(auctionPhase.getStartPrice());
									auctionPhase.setPhaseOurResult(auctionPhase.getStartPrice());
									// 如果是第一个阶段开始,就把拍卖状态改为进行中
									if (i == 0) {
										isFirstBegin = true;
									}
								}
								if (StringUtil.isEmpty(chatMessage.getNextPhaseId())) {
									// 如果是最后一个阶段结束,就把拍卖状态改为结束
									if (i == auctionPhases.size() - 1) {
										isEnd = true;
									}
								}
							}
							if (isFirstBegin) {
								mAuction.setStateCode(CommonConstants.STATE_BEGINNING);
							} else if (isEnd) {
								mAuction.setStateCode(CommonConstants.STATE_FINISHED);
							}
							updateView();
						}
					}
				} else if (MessageConstants.ACTION_MESSAGE_INFO_CHANGE.equals(action)) {
					// 更新竞拍人信息
					Auction auction = (Auction) intent.getSerializableExtra(CommonConstants.AUCTION);
					if (action != null && StringUtil.getDefaultString(mAuction.getAuctionId()).equals(auction.getAuctionId())) {
						updateAuctionInfo();
					}
				}
			}
		}
	};

	private void updateAuctionInfo() {
		// 先调用接口拉下最新的数据
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("auctionId", mAuction.getAuctionId());

		HttpService mHttpService = HttpService.getInstance();
		HttpAuctionServiceRx mHttpAuctionServiceRx = mHttpService.getRxGsonRetrofit().create(HttpAuctionServiceRx.class);
		final Call<ResponseBody> call = mHttpAuctionServiceRx.getMessageByWeb(params);
		mHttpService.executeHttp(mContext, call, new HttpService.HttpListener<String>() {
			@Override
			public void onStart() {
			}

			@Override
			public void onError(EbeiErrorCode errorCode) {
			}

			@Override
			public void onSuccess(final String result) {
				new AsyncTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {
						if (!StringUtil.isEmpty(result)) {
							try {
								Auction auction = new Gson().fromJson(result, Auction.class);
								mAuction = auction;
							} catch (Exception e) {}
						}
						return null;
					}

					@Override
					protected void onPostExecute(Void aVoid) {
						super.onPostExecute(aVoid);
						updateView();
					}
				}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
			}
		}, String.class, false);
	}

	@OnClick({R.id.view_bidder})
	void onBidderClick(View view) {

		if (auctionBidderNames.size() == 0) {
			return;
		}

		final CommonAlertDialogFragment bidderDialog = new CommonAlertDialogFragment();
		bidderDialog.setStyle(CommonAlertDialogFragment.DialogStyle.LIST);
		bidderDialog.setCancelable(false);
		bidderDialog.setTitle(getString(R.string.ebei_bidder));
		bidderDialog.setCancelButtonVisible(false);
		bidderDialog.setButtonText(getString(R.string.ebei_cancel), "");
		bidderDialog.setList(auctionBidderNames);
		bidderDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mAuctionBidder = mAuctionBidders.get(position);
				viewBidder.setTextValue(StringUtil.getDefaultString(mAuctionBidder.getBidderAuctionCode()) +" "+StringUtil.getDefaultString(mAuctionBidder.getBidderName()));
				ViewUtil.dismissDialog(bidderDialog);
			}
		});
		bidderDialog.setOkClickListener(new CommonAlertDialogFragment.OnBtnClickListener() {
			@Override
			public void onClick(View v, CommonAlertDialogFragment dialog) {
				ViewUtil.dismissDialog(dialog);
			}
		});
		bidderDialog.show(getSupportFragmentManager(), "CommonSelectDialogFragment");
	}

	@OnClick({R.id.btn_submit})
	void onSubmitClick(View view) {
		// 如果输入框有焦点,第一次点击提交先去掉焦点校验数据
		if (mIsNeedChange) {
			return;
		}

		if (CommonConstants.STATE_FINISHED.equals(mAuction.getStateCode())) {
			// 结束后不能再出价
			ViewUtil.toastMsg(mContext, "本阶段竞拍已结束！");
			return;
		}

		if (!getString(R.string.ebei_bid_mine).equals(mTitleName)) {
			if (mAuctionBidder == null || StringUtil.isEmpty(mAuctionBidder.getBidderAuctionCode())) {
				ViewUtil.toastMsg(mContext, getString(R.string.ebei_options_item, getString(R.string.ebei_bidder)));
				return;
			}
			// 需要按照房间存
			if (!StringUtil.isEmpty(mAuctionBidder.getBidderAuctionCode())) {
				SPManager.getInstance().put(mAuction.getAuctionId() + "_" + SPConstants.BIDDER_CODE, mAuctionBidder.getBidderAuctionCode());
			}
		}

		Map<String, String> params = new HashMap<String, String>();
		params.put("auctionId", mAuction.getAuctionId());//直播间Id
		params.put("phaseId", mAuctionPhase.getPhaseId());//阶段Id
		params.put("bidderAuctionCode", mAuctionBidder.getBidderAuctionCode());//竞拍code
		params.put("price", viewCurrentBid.getEditValue().replace(mUnit,"") + "");//倍数

		HttpService mHttpService = HttpService.getInstance();
		HttpAuctionServiceRx mHttpAuctionServiceRx = mHttpService.getRxGsonRetrofit().create(HttpAuctionServiceRx.class);
		Call<ResponseBody> call = null;
		if (getString(R.string.ebei_bid_mine).equals(mTitleName)) {
			call = mHttpAuctionServiceRx.doAuctionOurBid(params);
		} else {
			call = mHttpAuctionServiceRx.doAuctionTheirBid(params);
		}
		mHttpService.executeHttp(mContext, call, new HttpService.HttpListener<String>() {
			@Override
			public void onStart() {

			}

			@Override
			public void onError(EbeiErrorCode errorCode) {
				ViewUtil.toastMsg(mContext, errorCode.getErrorMsg());
			}

			@Override
			public void onSuccess(String o) {
				if (StringUtil.isEmpty(o)) {
					ViewUtil.toastMsg(mContext, R.string.ebei_submit_failure);
					return;
				} else {
					try {
						JSONObject jsonObject = new JSONObject(o);
						if ("-1".equals(jsonObject.optString("ErrorCode"))) {
							String msg = jsonObject.optString("ErrorMsg");
							if (StringUtil.isEmpty(msg)) {
								msg = getString(R.string.ebei_network_load_failure);
							}
							ViewUtil.toastMsg(mContext, msg);
							return;
						}
					} catch (Exception e) {}

				}

				ViewUtil.toastMsg(mContext, R.string.ebei_submit_success);
				finish();
			}
		}, String.class);
	}

	@OnClick({ R.id.btn_add1, R.id.btn_add2, R.id.btn_add3 })
	void onAddClick(View view) {
		if (view.getId() == R.id.btn_add1) {
			int number = 1;
			viewMultiple.setNumber(number);
			changeViewByNumber(number);
		} else if (view.getId() == R.id.btn_add2) {
			int number = 2;
			viewMultiple.setNumber(number);
			changeViewByNumber(number);
		} else {
			int number = 3;
			viewMultiple.setNumber(number);
			changeViewByNumber(number);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ebei_activity_bid);
		ButterKnife.bind(this);
		initView();
		registerBroadCast();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}

	private void registerBroadCast() {
		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction(MessageConstants.ACTION_MESSAGE_RECEIVE);
		myIntentFilter.addAction(MessageConstants.ACTION_MESSAGE_INFO_CHANGE);
		mContext.registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

	private void initView() {
		mContext = this;
		Intent intent = getIntent();
		if (intent != null) {
			mTitleName = intent.getStringExtra(CommonConstants.TITLE_NAME);
			mAuction = (Auction) intent.getSerializableExtra(CommonConstants.AUCTION);
		}

		tvTitle.setText(mTitleName);
		viewCurrentBid.setEditColor(getResources().getColor(R.color.ebei_red));
		viewRatio.setEditColor(getResources().getColor(R.color.ebei_red));
		etMultiple = (EditText) viewMultiple.findViewById(R.id.et_number);
		etMultiple.setTextColor(getResources().getColor(R.color.ebei_red));

		if (getString(R.string.ebei_bid_mine).equals(mTitleName)) {
			viewBidder.setVisibility(View.GONE);
		} else {
			viewBidder.setVisibility(View.VISIBLE);
		}

		viewMultiple.setBorder(0);
		viewMultiple.setOnNumberVariateListener(new NumberVariateView.OnNumberVariateListener() {

			@Override
			public void onAdd() {
				float number = viewMultiple.getNumber();
				changeViewByNumber(number);
			}

			@Override
			public void onMinus(boolean isBorder) {
				if (isBorder) {
					return;
				}
				float number = viewMultiple.getNumber();
				changeViewByNumber(number);
			}

			@Override
			public void onVariate(float number) {
				changeViewByNumber(number);
			}
		});

		viewCurrentBid.getValueEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					mLastModifyView = viewCurrentBid;
//					String str = getValue(viewCurrentBid.getEditValue(), mUnit, "");
//					if (viewCurrentBid.getValueEditText().getSelectionStart() == viewCurrentBid.getEditValue().length()) {
//						viewCurrentBid.getValueEditText().setSelection(str.length()-mUnit.length());
//					}
				} else {
					checkValue();
					mLastModifyView = null;
				}
			}
		});
		viewCurrentBid.getValueEditText().setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DEL) {
					if (mUnit.length() > 0) {
						int index = viewCurrentBid.getEditValue().indexOf(mUnit);
						if (index > -1) {
							int selectionStart = viewCurrentBid.getValueEditText().getSelectionStart();
							if (selectionStart < index) {
								return false;
							}
							String str = getValue(viewCurrentBid.getEditValue(), mUnit, "");
							if (str.length() > 0) {
								viewCurrentBid.setEditValue(str.substring(0, str.length() - 1) + mUnit);
								viewCurrentBid.getValueEditText().setSelection(str.length() - 1);
								return true;
							}
							if (str.length() == 0)
							{
								viewCurrentBid.setEditValue(str.substring(0, str.length()) + mUnit);
								viewCurrentBid.getValueEditText().setSelection(str.length());
								return true;
							}
						}
					}
				}
				return false;
			}
		});

		viewRatio.getValueEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					mLastModifyView = viewRatio;
//					String str = getValue(viewRatio.getEditValue(), "%", "");
//					viewRatio.getValueEditText().setSelection(str.length());
				} else {
					checkValue();
					mLastModifyView = null;
				}
			}
		});
		viewRatio.getValueEditText().setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DEL) {
					String mUnit = "%";
					if (mUnit.length() > 0) {
						int index = viewRatio.getEditValue().indexOf(mUnit);
						if (index > -1) {
							int selectionStart = viewRatio.getValueEditText().getSelectionStart();
							if (selectionStart < index) {
								return false;
							}
							String str = getValue(viewRatio.getEditValue(), mUnit, "");
							if (str.length() > 0) {
								viewRatio.setEditValue(str.substring(0, str.length() - 1) + mUnit);
								viewRatio.getValueEditText().setSelection(str.length() - 1);
								return true;
							}

							if (str.length() == 0)
							{
								viewRatio.setEditValue(str.substring(0, str.length()) + mUnit);
								viewRatio.getValueEditText().setSelection(str.length());
								return true;
							}
						}
					}
				}
				return false;
			}
		});

		updateView();
	}

	private void updateView() {
		mAuctionPhase = mAuction.getCurrentAuctionPhase();
		if (mAuctionPhase == null) {
			mAuctionPhase = new AuctionPhase();
		}
		if (getString(R.string.ebei_bid_mine).equals(mTitleName)) {
			mAuctionBidder = mAuction.getOurAuctionBidder();
		} else {
			mAuctionBidders = mAuction.getAuctionBidders();
			auctionBidderNames = new ArrayList<String>();
			for (int i = mAuctionBidders.size() - 1;i > -1; i--) {
				AuctionBidder auctionBidder = mAuctionBidders.get(i);
				if (auctionBidder.getIsUs()) {
					mAuctionBidders.remove(auctionBidder);
					continue;
				}
				auctionBidderNames.add(0, StringUtil.getDefaultString(auctionBidder.getBidderAuctionCode())+" "+StringUtil.getDefaultString(auctionBidder.getBidderName()));
			}
			mAuctionBidder = mAuction.getAuctionBidderByCode(SPManager.getInstance().get(mAuction.getAuctionId() + "_" + SPConstants.BIDDER_CODE, ""));
		}
		mUnit = StringUtil.getDefaultString(mAuctionPhase.getUnit());
		LandInfo mLandInfo = mAuction.getLandInfo();
		if (mLandInfo == null) {
			mLandInfo = new LandInfo();
		}
		rateDemo = mLandInfo.getChargeArea();
		if (StringUtil.isEmpty(rateDemo)) {
			rateDemo = "0";
		}

		if (!getString(R.string.ebei_bid_mine).equals(mTitleName)) {
			if (mAuctionBidder != null) {
				viewBidder.setTextValue(StringUtil.getDefaultString(mAuctionBidder.getBidderAuctionCode())+" "+StringUtil.getDefaultString(mAuctionBidder.getBidderName()));
			}
			else {
				if (auctionBidderNames!=null&&auctionBidderNames.size()>0) {
					mAuctionBidder = mAuctionBidders.get(0);
					viewBidder.setTextValue(StringUtil.getDefaultString(mAuctionBidder.getBidderAuctionCode())+" "+StringUtil.getDefaultString(mAuctionBidder.getBidderName()));
				}
			}
		}

		if (mAuctionPhase.isShowRate()) {
			viewRatio.setVisibility(View.VISIBLE);
			viewNewQuote.setIsDividerViewVisible(true);
		} else {
			viewRatio.setVisibility(View.GONE);
			viewNewQuote.setIsFooterViewVisible(true);
		}
		viewCurrentBid.setEditForNumberDecimal();
		viewRatio.setEditForNumberDecimal();

		if (mAuctionPhase != null) {
			String currentPrice = mAuctionPhase.getPhaseResult();
			if (StringUtil.isEmpty(currentPrice)) {
				currentPrice = mAuctionPhase.getStartPrice();//可以考虑第一次的时候是不是用起拍价/分母
			}
			viewNewQuote.setTextValue(StringUtil.getDefaultString(currentPrice) + mUnit + mAuctionPhase.getPhaseResultRateString(mAuctionPhase.getPhaseResultRate()));
			viewStep.setTextValue(mAuctionPhase.getStepLength() + mUnit);
		}

		float number = viewMultiple.getNumber();
		changeViewByNumber(number);
	}

	private boolean clearEditFocus() {
		View v = getCurrentFocus();
		if (v instanceof EditText) {
			v.clearFocus();
			return true;
		}
		return false;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			mIsNeedChange = mLastModifyView != null;
			clearEditFocus();
		}
		return super.dispatchTouchEvent(ev);
	}

	private void checkValue() {
		if (mLastModifyView == viewCurrentBid) {
			checkCurrentBidValue();
		} else if (mLastModifyView == viewRatio) {
			isCheckRatioValue();
		}
	}

	private void checkCurrentBidValue() {
		String startBid = getValue(viewNewQuote.getTextValue(), mUnit, "0");
		String currentBid = getValue(viewCurrentBid.getEditValue(), mUnit, "0");
		float number = 1;
		try {
			float diff = 0;
			if (AuctionPhase.QUOTES_TYPE_ADD.equals(mAuctionPhase.getQuotesTypeCode())) {
				diff = Float.valueOf(currentBid) - Float.valueOf(startBid);
			} else {
				diff = Float.valueOf(startBid) - Float.valueOf(currentBid);
			}
			if (diff >= 0) {
				number = get2Decimal(diff / Float.valueOf(mAuctionPhase.getStepLength()));
			}

		} catch (Exception e) {

		}

		viewMultiple.setNumber(number);
		changeViewByNumber(number);
	}

	private void isCheckRatioValue() {
		String startBid = getValue(viewNewQuote.getTextValue(), mUnit, "0");
		String radio = getValue(viewRatio.getEditValue(), "%", "0");
		float number = 1;
		try {
			float diff = 0;
			if (AuctionPhase.QUOTES_TYPE_ADD.equals(mAuctionPhase.getQuotesTypeCode())) {
				diff = Float.valueOf(rateDemo) * Float.valueOf(radio) / 100
						- Float.valueOf(startBid);
			} else {
				diff = Float.valueOf(startBid)
						- Float.valueOf(rateDemo) * Float.valueOf(radio) / 100;
			}
			if (diff >= 0) {
				number = get2Decimal(diff / Float.valueOf(mAuctionPhase.getStepLength()));
			}
		} catch (Exception e) {

		}

		viewMultiple.setNumber(number);
		changeViewByNumber(number);
	}

	private void changeViewByNumber(float number) {
		try {
			float currentBid = Float.valueOf(getValue(viewNewQuote.getTextValue(), mUnit, "0"));
			if (AuctionPhase.QUOTES_TYPE_ADD.equals(mAuctionPhase.getQuotesTypeCode())) {
				currentBid += Float.valueOf(mAuctionPhase.getStepLength()) * number;
			} else {
				currentBid -= Float.valueOf(mAuctionPhase.getStepLength()) * number;
			}
			viewCurrentBid.setEditValue(StringUtil.formatWithDecimal(currentBid + "", 2) + mUnit);
			if (mAuctionPhase.isShowRate()) {
				if (Float.valueOf(rateDemo)==0f)
				{
					viewRatio.setEditValue("%");
				}
				else {
					float ratio = Float.valueOf(currentBid) * 100 / Float.valueOf(rateDemo);
					viewRatio.setEditValue(StringUtil.formatWithDecimal(ratio + "", 2) + "%");
				}

			}

			// 如果本轮出价小于0,那么重新计算最接近的不为0倍数
			if (!AuctionPhase.QUOTES_TYPE_ADD.equals(mAuctionPhase.getQuotesTypeCode()) && currentBid < 0) {
				viewMultiple.setNumber(Float.valueOf(number - 1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String getValue(String content, String unit, String defaultValue) {
		if (unit.length() > 0 && content.length() > unit.length()) {
			int index = content.indexOf(unit);
			if (index > -1) {
				content = content.substring(0, index);
			}
		}
		else if (content.equals(unit)) {
			content = defaultValue;
		}
		return content;
	}

	private float get2Decimal(float number)
	{
		BigDecimal bg = new BigDecimal(number);
		float f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		return f1;
	}

}
